package com.angel.provider.sys.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.angel.core.base.BaseProviderImpl;
import com.angel.core.util.ResultParam;
import com.angel.core.util.StringUtils;
import com.angel.dao.health.SysOfficeMapper;
import com.angel.health.SysOffice;
import com.angel.health.SysOfficeExt;
import com.angel.provider.sys.SysManagerDepartProvider;

/**
 * 高亚林
 * 2016年9月20日下午5:05:26
 */
@Service
public class SysManagerDepartProviderImpl extends BaseProviderImpl<SysOffice>
		implements SysManagerDepartProvider {

	@Autowired
	private SysOfficeMapper mapper;
	
	//查询所有部门
	public List<SysOffice> findAllDepart(SysOffice office) {
		return mapper.findAllDepart(office);
	}

	//批量删除
	public ResultParam delete(String ids) {
		ResultParam param = new ResultParam();
		try {
			SysOffice office = new SysOffice();
			if(StringUtils.isEmpty(ids)){
				param.setCode("0");
				param.setMsg("请选择要删除的对象");
				return param;
			}
			String[] idarr = ids.split("&");
			for (String id : idarr) {
				office.setId(Integer.parseInt(id));
				office.setDelFlag("0");
				mapper.updateByPrimaryKeySelective(office);
			}
			param.setCode("1");
		} catch (NumberFormatException e) {
			e.printStackTrace();
			param.setCode("0");
			param.setMsg("删除失败");
			return param;
		}
		return param;
	}

	//删除单个用户
	public ResultParam deleteById(SysOffice office) {
		ResultParam param = new ResultParam();
		if(office == null){
			param.setCode("0");
			param.setMsg("请选择要删除的对象");
			return param;
		}
		office.setDelFlag("0");
		mapper.updateByPrimaryKeySelective(office);
		param.setCode("1");
		return param;
	}

	//保存
	public Map<String, String> save(SysOffice office) {
		Map<String, String> map = new HashMap<String, String>();
		if(office == null || office.getName() == null || office.getName() == ""){
			map.put("msg", "名称不能为空");
			map.put("code", "0");
			return map;
		}
		SysOffice sysOffice = new SysOffice();
		sysOffice.setName(office.getName());
		List<Map<String, Object>> list = mapper.findAll(sysOffice);
		if(list != null && list.size() > 0){
			map.put("msg", "部门已存在");
			map.put("code", "0");
			return map;
		}
		office.setCreateTime(new Date());
		office.setDelFlag("1");
		mapper.insert(office);
		//添加所有上级机构
		if(office.getParentId() == null){
			office.setParentIds("_"+office.getId()+"_");
		}else{
			//如果上级机构存在，获取上级机构
			SysOffice parentOffice = mapper.selectByPrimaryKey(office.getParentId());
			//获取上级机构的所有上级
			String parentIds = parentOffice.getParentIds();
			//用本机构的上级机构拼接上级机构的所有上级机构
			office.setParentIds(parentIds+office.getId()+"_");
		}
		mapper.updateByPrimaryKeySelective(office);
		map.put("msg", "添加成功");
		map.put("code", "1");
		return map;
	}

	//根据id查询
	public SysOfficeExt findUserById(SysOffice office) {
		List<SysOfficeExt> list = mapper.findUserById(office);
		if(list != null && list.size() > 0){
			return list.get(0);
		}
		return null;
	}

	//修改
	public Map<String, String> update(SysOffice office) {
		Map<String, String> map = new HashMap<String, String>();
		if(office == null || office.getName() == null || office.getName() == ""){
			map.put("msg", "名称不能为空");
			map.put("code", "0");
			return map;
		}
		office.setUpdateTime(new Date());
		mapper.updateByPrimaryKeySelective(office);
		map.put("msg", "修改成功");
		map.put("code", "1");
		return map;
	}


}
